package com.terren.spider.core.html.scheme.impl;

import java.util.HashSet;
import java.util.Set;

import com.terren.spider.entity.core.Entry;

import us.codecraft.webmagic.Page;

/**
 * 知乎企业用户搜索
 * @author chengyu
 *
 */
public class ZhihuOrgScheme extends BasicCustomScheme{
	/*@Override
	public Integer setRenderPage() {
	
		return 10;
	}*/
	@Override
	public Set<String> findHelpPaginationUrl(Page page, Entry entry) {
		if(page==null){
			return null;
		}
		Set<String> urlSet=new HashSet<>();
		String totalStr =page.getJson().jsonPath("$.paging.totals").get();
		String lbUrl = page.getUrl().get();
		Integer total = 0;
		if(totalStr!=null){
			total = Integer.parseInt(totalStr);
		}
		int pagNum = (int) Math.ceil(total/20.0);
		for(int i=0;i<pagNum;i++){
			String url = lbUrl+"?include=data%5B*%5D.comment_count%2Csuggest_edit%2Cis_normal%2Cthumbnail_extra_info%2Cthumbnail%2Ccan_comment%2Ccomment_permission%2Cadmin_closed_comment%2Ccontent%2Cvoteup_count%2Ccreated%2Cupdated%2Cupvoted_followees%2Cvoting%2Creview_info%3Bdata%5B*%5D.author.badge%5B%3F(type%3Dbest_answerer)%5D.topics&offset="
					+i*20+"&limit=20&sort_by=created";
			urlSet.add(url);
		}
		return urlSet;
	}
	/*@Override
	public Set<String> findPaginationUrl(Page page, Entry entry) {
		List<String> pgList = new ArrayList<String>();
		Selectable pagediv = page.getHtml().xpath("//div[@class='Pagination']");
		int pageOn = Integer.parseInt(pagediv.xpath("//button[@disabled]/text()").get());//当前页码
		String nextPage = pagediv.xpath("//button[@class='PaginationButton-next']/text()").get();
		if(nextPage !=null&&nextPage.equals("下一页")){
			String nextUrl ="";
			if(page.getUrl().get().contains("posts")){//文章列表分页
				nextUrl = StringUtils.substringBefore(page.getUrl().get(), "posts")+"posts?page="+(pageOn+1);
			}else if(page.getUrl().get().contains("answers")){//回答列表分页
				nextUrl = StringUtils.substringBefore(page.getUrl().get(), "answers")+"answers?page="+(pageOn+1);
			}
			pgList.add(nextUrl);
		}
		
		Set<String> resultSet = new HashSet<String>();
		resultSet.addAll(pgList);
		return resultSet;
	}
	*/
	@Override
	public boolean useAbuyunProxy() {
		
		return true;
	}
	
	/*@Override
	public Map<String, String> getCookie(Entry entry) {
		Map<String, String> cookies = new HashMap<>();
		cookies.put("_xsrf","360a4111-f685-4dd4-a19b-8d650e2e0161");
		cookies.put("d_c0","\"AIDmnMpFvw2PTr5joxy79WMXVNx28QdGHZ8=|1528954387\"");
		cookies.put("q_c1","1e138a9fdd0e4995a95386ea212e3c4c|1528954387000|1528954387000");
		cookies.put("capsion_ticket","\"2|1:0|10:1528954394|14:capsion_ticket|44:NjIxYzk2OWI3NjI2NDI3ODgyOTJiZmQ5MTJjYTJlZTM=|3123ed17aeca15f4aa61122d33bab86e72d4e602732f961b17f9a75737077c81\"");
		cookies.put("_zap","543bb84e-b905-4c53-84b8-e8382fd22395");
		cookies.put("tgw_l7_route","ec452307db92a7f0fdb158e41da8e5d8");
		return cookies;
	}*/
	/*@Override
	public Integer setTimeWait() {
		// TODO Auto-generated method stub
		return 10;
	}*/
}
